package com.xoa.util.cms.directive;

import com.xoa.dao.portalColumn.PortalColumnMapper;
import com.xoa.dao.cms.SiteMapper;
import com.xoa.model.portalColumn.PortalColumn;
import com.xoa.model.cms.Site;
import com.xoa.service.cms.RenderingService;
import com.xoa.util.common.StringUtils;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

public class CMSLocationDirective implements TemplateDirectiveModel{
    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {

        //获取标签内的栏目标识
        String chnlIdentity = StringUtils.getString(map.get("id"));

        Writer writer = environment.getOut();

        //获取栏目Mapper
        PortalColumnMapper portalColumnMapper = (PortalColumnMapper) environment.getCustomAttribute("portalColumnMapper");
        RenderingService renderingService = (RenderingService) environment.getCustomAttribute("renderingService");
        //是否预览模式
        boolean preview = (Boolean) environment.getCustomAttribute("preview");
        Object channelId = environment.getCustomAttribute("channelId");

        PortalColumn chnl = null;
        if(chnlIdentity!=null && !"".equals(chnlIdentity)){
            chnl = portalColumnMapper.selectByColumnMark(chnlIdentity);
        }

        if(chnl==null){
            if(null!=channelId){
                chnl = portalColumnMapper.selectByPrimaryKey((int)channelId);
            }else{
                return;
            }
        }
        String channelPath = renderingService.getCloumnAbsolutePath(chnl.getColumnId());

        SiteMapper siteMapper = (SiteMapper) environment.getCustomAttribute("siteMapper");
        Site site = siteMapper.selectByPrimaryKey(chnl.getPortalId());

        String firstIndexUrl="";
        if(null!=chnl){
            firstIndexUrl=channelPath+"index."+site.getPubFileExt();
        }else{
            firstIndexUrl = "./index."+site.getPubFileExt();
        }
        if(preview){
            firstIndexUrl= "/site/preview.action?siteId="+chnl.getPortalId();
        }
        String firstIndexPath="";
        Map data = new HashMap();
        data.put("url",firstIndexUrl);
        data.put("name","首页");
        environment.setCustomAttribute("loopObj", data);
        if(templateDirectiveBody!=null){
            templateDirectiveBody.render(writer);
        }
        environment.setCustomAttribute("loopObj", null);
        String path = chnl.getPath();
        String[] paths = path.split("/");
        String chalPath="";
        String chalName="";

        for (int i=1;i<paths.length;i++) {
            if(i==paths.length-1){
                chalPath="index."+site.getPubFileExt();
                chalName=chnl.getColumnName();
                if(preview){
                    chalPath=  "/site/preview?siteId="+chnl.getPortalId()+"&channelId="+chnl.getColumnId();
                }
            }else{
                String [] ids = (paths[i]).split("\\.");
                PortalColumn info = portalColumnMapper.selectByPrimaryKey(Integer.parseInt(ids[0]));
                String realPath = renderingService.getCloumnAbsolutePath(info.getColumnId());
                chalPath=realPath+"index."+site.getPubFileExt();
                chalName=info.getColumnName();
                if(preview){
                    chalPath= "/site/preview?siteId="+chnl.getPortalId()+"&channelId="+info.getColumnId();
                }
            }

            data.put("url",chalPath);
            data.put("name",chalName);
            environment.setCustomAttribute("loopObj", data);
            if(templateDirectiveBody!=null){
                templateDirectiveBody.render(writer);
            }
            environment.setCustomAttribute("loopObj", null);
        }
    }
}
